// pages/detail/detail.js
import {
  imgFormat
} from '../../utils/imgFormat';
import axios, {
  baseUrl
} from '../../utils/axios';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: {},
    list: [],
    phone: '',
    mtqDuration:0,
    mtqCode:"",
    distance:0,
    roomIds:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options && options.companyId) {
      if(options.mtqCode){
        this.setData({
          mtqCode:options.mtqCode,
          mtqDuration:options.mtqDuration,
          roomIds:options.roomIds
        })
      }
      this.setData({
        distance:options.distance||0
      })
      this.queryCompanyInfo(options.companyId);
    } else {
      wx.switchTab({
        url: `/pages/home/home`,
      })
    }
  },
  async queryCompanyInfo(id){
    const item = await axios.get("/app/dpList",{
      id
    }).then(res=>{
      return res.data[0]
    })
    wx.setNavigationBarTitle({
      title: item.name
    })
    item.distance = this.data.distance;
    this.setData({
      item,
      phone: item.phone_list[0].phone
    })
    this.getList(item)
  },
  getRoomUsaged() {
    const all = [];
    this.data.list.forEach(item=>{
      all.push(new Promise(resolve=>{
        axios.get('/app/getRoomUsaged', {roomId: item.ID}).then(res=>{
          resolve(res);
        })
      }))
    })
    const list_bk = JSON.parse(JSON.stringify(this.data.list))
    Promise.all(all).then(res=>{
      res.forEach((item,index)=>{
        if (item.data.length == 0 && list_bk[index].status == 1) {
          list_bk[index].status = 0;
        }
      })
      this.setData({
        list:list_bk
      })
    })
  },
  async getList(item) {
    wx.showLoading({
      title: '加载中',
    })
    try {
      const {
        data,
        use = []
      } = await axios.get("/app/roomList", {
        company_id: item.ID
      })
      let list = (data || []).map(item => {
        item.banners = imgFormat(item.banners);
        item.endTime = use.find(ite=>ite.id === item.ID)?.timer;
        return item
      })
      if(this.data.roomIds!=""){
        const rds = this.data.roomIds.split(",");
        list = list.filter(item=>{
          return rds.includes(item.ID);
        })
      }
      this.setData({
        list
      })
      this.getRoomUsaged()
    } finally {
      wx.hideLoading()
    }

  },
  goToDetail(e) {
    const dataset = e.currentTarget.dataset.item;
    let item = Object.assign(dataset, {
      shopName: this.data.item.name
    })
    item = JSON.stringify(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: `/pages/tea-detail/tea-detail?item=${item}&mtqCode=${this.data.mtqCode}&mtqDuration=${this.data.mtqDuration}`,
    });
  },
  getCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.phone //仅为示例，并非真实的电话号码
    })
  },
  getLocation() {
    wx.openLocation({
      latitude: this.data.item.coord.lat,
      longitude: this.data.item.coord.lng,
      scale: 18
    })
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})